/*
 * EntryWindow.java
 *
 * Created on __DATE__, __TIME__
 */

package gui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import core.Category;
import core.Core;
import core.UserEntry;

/**
 * 
 * @author __USER__
 */
public class EntryWindow extends javax.swing.JDialog {

	private Core core = null;
	private List<Category> categories = null;
	private PrincipalWindow parent = null;
	private UserEntry userEntry = null;
	private JCalendar jcalendar = null;

	/** Creates new form EntryWindow */
	public EntryWindow(PrincipalWindow parent, boolean modal) {
		super(parent, modal);
		this.parent = parent;
		initComponents();		

		Border etchedBorder = BorderFactory.createEtchedBorder();
		Border emptyBorder = BorderFactory.createEmptyBorder(10, 10, 10, 10);
		Border compoundBorder = BorderFactory.createCompoundBorder(
				etchedBorder, emptyBorder);
		
		/*
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		*/
		jcalendar = new JCalendar();
		Font f = jcalendar.getFont().deriveFont(10);
		jcalendar.setFont(f);
		SwingUtilities.updateComponentTreeUI(jcalendar);
		jPanel1.setLayout(new GridLayout(1,1,1,1));
		jPanel1.setPreferredSize(new Dimension(350, 250));
		SwingUtilities.updateComponentTreeUI(jcalendar);
		jPanel1.add(jcalendar);
		/*
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}*/
	
		Date curDate = new Date();
		SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy");
		dateLabel.setText(format.format(curDate));

		configure();
	}

	private void selectGoodItem() {
		String catName = new String();
		categories = core.getCategories();
		Iterator<Category> i = categories.iterator();
		while (i.hasNext()) {
			Category current = i.next();
			if (current.getId() == userEntry.getCategoryId()) {
				catName = current.getName();
				break;
			}
		}
		for (int j = 0; j < categories.size() + 1; ++j) {
			String s = (String) category.getItemAt(j);
			System.out.println("cat:" + s);
			if (s.equals(catName)) {
				category.setSelectedIndex(j);
				break;
			}
		}
	}

	public void setUserEntry(UserEntry entry) {
		userEntry = entry;
		amountValue.setText(Float.toString(Math.abs(entry.getValue())));
		if (entry.getValue() < 0)
			debitBoutton.setSelected(true);
		else
			creditButton.setSelected(true);
		commentText.setText(entry.getComment());

		selectGoodItem();
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		dateLabel.setText(format.format(entry.getDate()));
		jcalendar.setDate(entry.getDate());
	}

	public void setComboBox() {
		this.parent.refreshTable();
		core = Core.get();
		category.removeAllItems();
		categories = core.getCategories();
		category.addItem(new String("None"));
		Iterator<Category> i = categories.iterator();
		while (i.hasNext()) {
			Category current = i.next();
			category.addItem(current.getName());
		}

		if (userEntry != null)
			selectGoodItem();
	}

	public void configure() {
		setComboBox();
	}

	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		buttonGroup1 = new javax.swing.ButtonGroup();
		jPanel2 = new javax.swing.JPanel();
		jPanel3 = new javax.swing.JPanel();
		jPanel1 = new javax.swing.JPanel();
		dateLabel = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		category = new javax.swing.JComboBox();
		jLabel3 = new javax.swing.JLabel();
		amountValue = new javax.swing.JTextField();
		jPanel4 = new javax.swing.JPanel();
		debitBoutton = new javax.swing.JRadioButton();
		creditButton = new javax.swing.JRadioButton();
		jLabel4 = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		commentText = new javax.swing.JEditorPane();
		jLabel1 = new javax.swing.JLabel();
		closeButton = new javax.swing.JButton();
		createButton = new javax.swing.JButton();
		createAndcloseButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jPanel2.setBackground(new java.awt.Color(141, 178, 227));

		jPanel3.setBackground(new java.awt.Color(141, 178, 227));
		jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

		jPanel1.setBackground(new java.awt.Color(255, 51, 0));
		jPanel1.setMaximumSize(new java.awt.Dimension(200, 200));
		jPanel1.setOpaque(false);
		jPanel1.setPreferredSize(new java.awt.Dimension(220, 220));

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 362,
				Short.MAX_VALUE));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 192,
				Short.MAX_VALUE));

		dateLabel.setText("Samedi 12 mars 2011 ");

		jLabel2.setText("Payment Method");

		category.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"Item 1", "Item 2", "Item 3", "Item 4" }));

		jLabel3.setText("Amount");

		amountValue.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
		amountValue.setText("0,00");

		buttonGroup1.add(debitBoutton);
		debitBoutton.setSelected(true);
		debitBoutton.setText("Debit");
		debitBoutton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				debitBouttonActionPerformed(evt);
			}
		});

		buttonGroup1.add(creditButton);
		creditButton.setText("Credit");

		javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(
				jPanel4);
		jPanel4.setLayout(jPanel4Layout);
		jPanel4Layout
				.setHorizontalGroup(jPanel4Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel4Layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(debitBoutton)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												116, Short.MAX_VALUE)
										.addComponent(creditButton)
										.addGap(23, 23, 23)));
		jPanel4Layout
				.setVerticalGroup(jPanel4Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel4Layout
										.createSequentialGroup()
										.addGroup(
												jPanel4Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																debitBoutton)
														.addComponent(
																creditButton))
										.addContainerGap(36, Short.MAX_VALUE)));

		jLabel4.setText("Comment");

		jScrollPane1.setViewportView(commentText);

		jLabel1.setBackground(new java.awt.Color(102, 255, 51));
		jLabel1.setIcon(new javax.swing.ImageIcon(
				"icones/addCategory.png")); // NOI18N
		jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				callAddCategoryWindow(evt);
			}
		});

		javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(
				jPanel3);
		jPanel3.setLayout(jPanel3Layout);
		jPanel3Layout
				.setHorizontalGroup(jPanel3Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel3Layout
										.createSequentialGroup()
										.addGroup(
												jPanel3Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel3Layout
																		.createSequentialGroup()
																		.addGap(23,
																				23,
																				23)
																		.addComponent(
																				jPanel1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				362,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(18,
																				18,
																				18)
																		.addGroup(
																				jPanel3Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addGroup(
																								jPanel3Layout
																										.createSequentialGroup()
																										.addGap(97,
																												97,
																												97)
																										.addComponent(
																												dateLabel))
																						.addGroup(
																								jPanel3Layout
																										.createSequentialGroup()
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addGroup(
																												jPanel3Layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING)
																														.addComponent(
																																jPanel4,
																																javax.swing.GroupLayout.PREFERRED_SIZE,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																javax.swing.GroupLayout.PREFERRED_SIZE)
																														.addGroup(
																																jPanel3Layout
																																		.createSequentialGroup()
																																		.addGroup(
																																				jPanel3Layout
																																						.createParallelGroup(
																																								javax.swing.GroupLayout.Alignment.LEADING)
																																						.addComponent(
																																								jLabel2)
																																						.addComponent(
																																								jLabel3))
																																		.addGap(309,
																																				309,
																																				309)
																																		.addGroup(
																																				jPanel3Layout
																																						.createParallelGroup(
																																								javax.swing.GroupLayout.Alignment.LEADING)
																																						.addGroup(
																																								jPanel3Layout
																																										.createSequentialGroup()
																																										.addComponent(
																																												category,
																																												javax.swing.GroupLayout.PREFERRED_SIZE,
																																												javax.swing.GroupLayout.DEFAULT_SIZE,
																																												javax.swing.GroupLayout.PREFERRED_SIZE)
																																										.addPreferredGap(
																																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																										.addComponent(
																																												jLabel1))
																																						.addComponent(
																																								amountValue,
																																								javax.swing.GroupLayout.PREFERRED_SIZE,
																																								83,
																																								javax.swing.GroupLayout.PREFERRED_SIZE)))))))
														.addGroup(
																jPanel3Layout
																		.createSequentialGroup()
																		.addGap(47,
																				47,
																				47)
																		.addComponent(
																				jLabel4))
														.addGroup(
																jPanel3Layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				jScrollPane1,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				877,
																				Short.MAX_VALUE)))
										.addContainerGap()));
		jPanel3Layout
				.setVerticalGroup(jPanel3Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel3Layout
										.createSequentialGroup()
										.addGroup(
												jPanel3Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel3Layout
																		.createSequentialGroup()
																		.addGroup(
																				jPanel3Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								false)
																						.addGroup(
																								jPanel3Layout
																										.createSequentialGroup()
																										.addGap(22,
																												22,
																												22)
																										.addComponent(
																												dateLabel)
																										.addGap(37,
																												37,
																												37)
																										.addGroup(
																												jPanel3Layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.BASELINE)
																														.addComponent(
																																jLabel2)
																														.addComponent(
																																category,
																																javax.swing.GroupLayout.PREFERRED_SIZE,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																javax.swing.GroupLayout.PREFERRED_SIZE))
																										.addGap(35,
																												35,
																												35))
																						.addGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								jPanel3Layout
																										.createSequentialGroup()
																										.addContainerGap(
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												Short.MAX_VALUE)
																										.addComponent(
																												jLabel1,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												21,
																												javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addGap(36,
																												36,
																												36)))
																		.addGroup(
																				jPanel3Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								jLabel3)
																						.addComponent(
																								amountValue,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addGap(18,
																				18,
																				18)
																		.addComponent(
																				jPanel4,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE))
														.addGroup(
																jPanel3Layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				jPanel1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				192,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jLabel4,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												16,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jScrollPane1,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												85, Short.MAX_VALUE)
										.addGap(19, 19, 19)));

		closeButton.setText("Close");
		closeButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				closeEntryWindow(evt);
			}
		});
		closeButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				closeButtonActionPerformed(evt);
			}
		});

		createButton.setText("Create");
		createButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				createEntry(evt);
			}
		});

		createAndcloseButton.setText("Create and close");
		createAndcloseButton
				.addMouseListener(new java.awt.event.MouseAdapter() {
					public void mouseClicked(java.awt.event.MouseEvent evt) {
						callCreateAndClose(evt);
					}
				});

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout
				.setHorizontalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel2Layout
																		.createSequentialGroup()
																		.addGap(48,
																				48,
																				48)
																		.addComponent(
																				closeButton)
																		.addGap(120,
																				120,
																				120)
																		.addComponent(
																				createButton)
																		.addGap(187,
																				187,
																				187)
																		.addComponent(
																				createAndcloseButton))
														.addGroup(
																jPanel2Layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				jPanel3,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addContainerGap(30, Short.MAX_VALUE)));
		jPanel2Layout
				.setVerticalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addGap(22, 22, 22)
										.addComponent(
												jPanel3,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(18, 18, 18)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																closeButton)
														.addComponent(
																createButton)
														.addComponent(
																createAndcloseButton))
										.addContainerGap(28, Short.MAX_VALUE)));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)
						.addComponent(jPanel2,
								javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE)));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void callCreateAndClose(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:
		this.createEntry(evt);
		this.dispose();
	}

	public void callAddCategoryWindow(java.awt.event.MouseEvent evt) {
		AddCategory addCategoryWindow = new AddCategory(this.parent, true);
		addCategoryWindow.setFather(this);
		addCategoryWindow.setVisible(true);
	}

	public void setActionAndCloseText(String s) {
		createAndcloseButton.setText(s);
	}

	public javax.swing.JButton getCreateButton() {
		return createButton;
	}

	public javax.swing.JButton getCreateAndcloseButton() {
		return createAndcloseButton;
	}

	public void setCreateAndcloseButton(javax.swing.JButton createAndcloseButton) {
		this.createAndcloseButton = createAndcloseButton;
	}

	public void setCreateButton(javax.swing.JButton createButton) {
		this.createButton = createButton;
	}

	public javax.swing.JTextField getAmountValue() {
		return amountValue;
	}

	public void setAmountValue(javax.swing.JTextField amountValue) {
		this.amountValue = amountValue;
	}

	public javax.swing.JComboBox getCategory() {
		return category;
	}

	public void setCategory(javax.swing.JComboBox category) {
		this.category = category;
	}

	public javax.swing.JEditorPane getCommentText() {
		return commentText;
	}

	public void setCommentText(javax.swing.JEditorPane commentText) {
		this.commentText = commentText;
	}

	public javax.swing.JRadioButton getCreditButton() {
		return creditButton;
	}

	public void setCreditButton(javax.swing.JRadioButton creditButton) {
		this.creditButton = creditButton;
	}

	public javax.swing.JRadioButton getDebitBoutton() {
		return debitBoutton;
	}

	public void setDebitBoutton(javax.swing.JRadioButton debitBoutton) {
		this.debitBoutton = debitBoutton;
	}

	private void closeEntryWindow(java.awt.event.MouseEvent evt) {
		this.dispose();
	}

	private void createEntry(java.awt.event.MouseEvent evt) {
		String error = new String();
		String valueStr = amountValue.getText();
		valueStr = valueStr.replace(',', '.');
		float value = 0;
		try {
			value = Float.parseFloat(valueStr);
			if (value <= 0) {
				error = "Value must be greater than zero";
			}
		} catch (Exception e) {
			error = "Bad number format";
		}

		if (debitBoutton.isSelected())
			value *= -1;

		if (error.isEmpty()) {
			int catId = (category.getSelectedIndex() == 0) ? 0 : categories
					.get(category.getSelectedIndex() - 1).getId();

			if (userEntry != null) {
				userEntry.setCategoryId(catId);
				userEntry.setComment(commentText.getText());
				userEntry.setDate(jcalendar.getDate());
				userEntry.setValue(value);
				userEntry.save();
			} else
				core.addEntry(value, commentText.getText(), catId,
						jcalendar.getDate());
			this.parent.refreshTable();
		} else {
			DialogWindow dw = new DialogWindow(null, true, "Error", error, 1,
					true);
			dw.setVisible(true);
		}
	}

	private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {
		System.out.println("closeButtonActionPerformed");
		this.dispose();
	}

	private void debitBouttonActionPerformed(java.awt.event.ActionEvent evt) {
	}

	/**
	 * @param args
	 *            the command line arguments
	 */

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JTextField amountValue;
	private javax.swing.ButtonGroup buttonGroup1;
	private javax.swing.JComboBox category;
	private javax.swing.JButton closeButton;
	private javax.swing.JEditorPane commentText;
	private javax.swing.JButton createAndcloseButton;
	private javax.swing.JButton createButton;
	private javax.swing.JRadioButton creditButton;
	private javax.swing.JLabel dateLabel;
	private javax.swing.JRadioButton debitBoutton;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JPanel jPanel4;
	private javax.swing.JScrollPane jScrollPane1;
	// End of variables declaration//GEN-END:variables

}